package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantFields;
import cn.yuanqiao.archive.appManager.domain.TenantMdataGroup;
import cn.yuanqiao.archive.appManager.domain.TenantMdataList;
import cn.yuanqiao.common.core.domain.TreeSelect;
import cn.yuanqiao.archive.appManager.mapper.TenantFieldsMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantMdataGroupMapper;
import cn.yuanqiao.archive.appManager.mapper.TenantMdataListMapper;
import cn.yuanqiao.archive.appManager.service.ITenantMdataGroupService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjj
 * @date 2023/6/21
 */
@Service
public class TenantMdataGroupServiceImpl implements ITenantMdataGroupService {
    @Autowired
    private TenantMdataGroupMapper yqdaMdataGroupMapper;
    @Autowired
    private TenantFieldsMapper yqFieldsMapper;
    @Autowired
    private TenantMdataListMapper yqdaMdataListMapper;

    /**
     * 查询元数据方案
     *
     * @param ID 元数据方案主键
     * @return 元数据方案
     */
    @Override
    public TenantMdataGroup selectTenantMdataGroupByID(Long ID)
    {
        return yqdaMdataGroupMapper.selectTenantMdataGroupByID(ID,new TenantMdataGroup().getTableName());
    }

    /**
     * 查询元数据方案列表
     *
     * @param yqdaMdataGroup 元数据方案
     * @return 元数据方案
     */
    @Override
    public List<TenantMdataGroup> selectTenantMdataGroupList(TenantMdataGroup yqdaMdataGroup)
    {
        return yqdaMdataGroupMapper.selectTenantMdataGroupList(yqdaMdataGroup);
    }

    /**
     * 新增元数据方案
     *
     * @param yqdaMdataGroup 元数据方案
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTenantMdataGroup(TenantMdataGroup yqdaMdataGroup)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        yqdaMdataGroup.setCreatorId(user.getUserId());
        yqdaMdataGroup.setCreateTime(DateUtils.getNowDate());
        yqdaMdataGroup.setIsUse(0);
        int r= yqdaMdataGroupMapper.insertTenantMdataGroup(yqdaMdataGroup);
        if(r>0){
            TenantFields yqFields = new TenantFields();
            List<TenantFields> getGroupDataList = new ArrayList<>();
            yqFields.setIsCommon("1");
            //yqfields.setType("0"); //0件 1卷 2电子文件 3项目
            String type = yqdaMdataGroup.getType() == 3 ? ("0,1,2,3"):(yqdaMdataGroup.getType() == 1 ? ("0,1,2"):("0,2"));
            yqFields.setType(type);
            getGroupDataList.addAll(yqFieldsMapper.getDataByIds(yqFields));

            //查询件的公共字段
            /*getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            yqfields.setType("2");
            getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            if(TenantMdataGroup.getType() == 1 || TenantMdataGroup.getType() == 3){
                yqfields.setType("1");
                getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            }
            if(TenantMdataGroup.getType() == 3){
                yqfields.setType("3");
                getGroupDataList.addAll(yqfieldsMapper.getDataByIds(yqfields));
            }*/
            for(TenantFields data:getGroupDataList){
                //添加元数据明细
                TenantMdataList mDataList = new TenantMdataList();
                mDataList.setFieldCaption(data.getFieldCaption());
                mDataList.setGroupId(yqdaMdataGroup.getId());
                mDataList.setFieldName(data.getFieldName());
                mDataList.setFieldType(Integer.valueOf(data.getFieldType()));
                mDataList.setDataLength(Integer.valueOf(data.getFieldLength()));
                mDataList.setType(Integer.valueOf(data.getType()));
                mDataList.setShowType(Integer.valueOf(data.getShowType()));
                mDataList.setIsSystem(1);

                mDataList.setDefaultValue(data.getDefaultVal());
                mDataList.setIsNoEmpty("true".equals(data.getNullable()) ? 0 : 1);

                //mDataList.setIsInput("true".equals(data.getNullAble()) ? 0 : 1);
                mDataList.setIsInput(0);
                mDataList.setIsUpdate(0);
                mDataList.setIsQuery(0);
                mDataList.setIsArchiveNo(0);
                // mDataList.setIsNoEmpty(0);
                mDataList.setIsHidden(1);
                mDataList.setIsExport(1);
                mDataList.setCheckChongfu(0);
                mDataList.setIsImport(0);
                // mDataList.setShowType(4);
                mDataList.setZsfl("1");
                mDataList.setThWidth(StringUtils.isNotEmpty(data.getThWidth()) ? Integer.valueOf(data.getThWidth()):null);
                mDataList.setThAlign(data.getThAlign());
                mDataList.setCreatorId(user.getUserId());
                mDataList.setCreateTime(DateUtils.getNowDate());
                yqdaMdataListMapper.insertTenantMdataList(mDataList);
            }

        }
        return r;
    }

    /**
     * 修改元数据方案
     *
     * @param yqdaMdataGroup 元数据方案
     * @return 结果
     */
    @Override
    public int updateTenantMdataGroup(TenantMdataGroup yqdaMdataGroup)
    {
        return yqdaMdataGroupMapper.updateTenantMdataGroup(yqdaMdataGroup);
    }

    /**
     * 删除元数据方案信息
     *
     * @param ID 元数据方案主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTenantMdataGroupByID(Long ID)
    {
        yqdaMdataListMapper.deleteTenantMdataListByGroupId(ID,new TenantMdataList().getTableName());
        //relatedelementMapper.deleteHdListRelatedelementByGroupId(ID);
        //subelementMapper.deleteHdListSubelementByGroupId(ID);
        return yqdaMdataGroupMapper.deleteTenantMdataGroupByID(ID,new TenantMdataGroup().getTableName());
    }

    @Override
    public List<TreeSelect> selectGroupTreeList(TenantMdataGroup yqdaMdataGroup) {
        List<TenantMdataGroup> list = selectTenantMdataGroupList(yqdaMdataGroup);
        TreeSelect rootTree = new TreeSelect();
        rootTree.setId(0L);
        rootTree.setLabel("元数据方案");
        List<TreeSelect> chilTrees = new ArrayList<>();
        for(TenantMdataGroup group:list){
            TreeSelect chilTree = new TreeSelect();
            chilTree.setId(group.getId());
            chilTree.setLabel(group.getName());
            chilTree.setType(group.getType());
            chilTree.setIsUse(group.getIsUse());
            chilTrees.add(chilTree);
        }
        rootTree.setChildren(chilTrees);
        List<TreeSelect> treeSelects = new ArrayList<>();
        treeSelects.add(rootTree);
        return treeSelects;
    }

    @Override
    public List<TenantMdataGroup> copyGroupList(Long groupId, Integer type) {
        TenantMdataGroup mdataGroup = new TenantMdataGroup();
        mdataGroup.setId(groupId);
        mdataGroup.setType(type);
        return yqdaMdataGroupMapper.copyGroupList(mdataGroup);
    }
}
